package com.xuxin.queue.model;

import com.xuxin.queue.bean.PlayerBean;
import com.xuxin.queue.bean.QueueBean;
import com.xuxin.queue.contract.QueueContract;
import com.xuxin.queue.network.Retrofit;
import com.xuxin.queue.network.RetrofitApi;
import com.xuxin.queue.presenter.QueuePresenter;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.observers.DisposableObserver;
import io.reactivex.rxjava3.schedulers.Schedulers;

/**
 * Created by xu_xin
 * Created on 2021/6/16 0016
 */
public class QueueModel implements QueueContract.Model {
    @Override
    public void getQueuingInfo(QueuePresenter presenter, String url) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .getQueuingInfo(url + "/api/services/app/Mc_Queue/QueuingInfo")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DisposableObserver<QueueBean>() {
                    @Override
                    public void onNext(@NonNull QueueBean queueBean) {
                        if (queueBean.isSuccess()) {
                            presenter.onSuccess(queueBean);
                        } else {
                            presenter.onError(queueBean);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        presenter.onComplete();
                    }
                });
    }

    @Override
    public void getPlayer(QueuePresenter presenter, String url) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .getPlayer(url + "/api/services/app/Mc_PlayQueue/GetPlayer")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DisposableObserver<PlayerBean>() {
                    @Override
                    public void onNext(@NonNull PlayerBean playerBean) {
                        if (playerBean.isSuccess()) {
                            presenter.onSuccess(playerBean);
                        } else {
                            presenter.onError(playerBean);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        presenter.onComplete();
                    }
                });
    }
}